// lib.c
//
//	look declaration at lib.h
//
//

#include "lib.h"
#include "syscall.h"

// for in/output
void sb_printf(char *str) {
	int size = sb_strlen(str);
	OpenFileId output = ConsoleOutputId;

	Write(str, size, output);
}

void sb_scanf(char *dest) {
	int i = 0;
	OpenFileId input = ConsoleInputId;

	do {

		Read(&dest[i], 1, input);

	} while (dest[i++] != '\n');

}

// for character
//\f 换页(FF) 012
//\n 换行(LF) 010
//\r 回车(CR) 013
//\t 水平制表(HT) 009
//\v 垂直制表(VT) 011
int sb_isspace(char c) {
	return (unsigned int) (c - 9) < 5u || c == ' ';
}

// for string process
int sb_strlen(char *str) {
	int len = 0;
	sb_assert(str != NULL);

	while (*(str + (len++)) != '\0')
		;
	return len;
}

int sb_strcp(char *str, char *destStr) {
	int index = 0;
/*
	int len_in = sb_strlen(str);
	int len_out = sb_strlen(destStr);

	while (*(str + (index++)) != '\0')
		*(destStr + index) = *(str + index);
*/
        do {
            *(destStr + index) = *(str + index);
        } while (*(str + (index++)));
	return index;
}

int sb_strcmp(char *str1, char *str2) {
	int ret = 0;
	while (!(ret = *(unsigned char *) str1 - *(unsigned char *) str2) && *str2) {
		++str1;
		++str2;
	}
	if (ret < 0)
		ret = -1;
	else if (ret > 0)
		ret = 1;
	return (ret);
}

int sb_strclr(char *str) {
	int index = 0;

	sb_assert(str != NULL);

	while (*(str + index) != '\n')
		*(str + index) = '\0';

	return index;
}
